package com.itheima.bookdao;

import com.itheima.domain.Book;

import java.util.ArrayList;

public class Book2Dao implements BaseBookDao {

    private static ArrayList<Book> list = new ArrayList<Book>();

    public Book[] getAllBooks() {

        Book[] books = new Book[list.size()];

        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);

            books[i] = book;

        }
        return books;
    }

    public boolean addBook(Book book){
        list.add(book);
        return true;
    }

    public void deleteBook(String bookId){

        int index = -1;

        for (int i = 0; i < list.size(); i++) {
            Book bookArr = list.get(i);
            if(bookArr != null){
                if(bookArr.getBookId().equals(bookId)){
                    index = i;
                    break;
                }
            }
        }
        if(index != -1){
            list.remove(index);
        }

    }

    public void updateBook(Book book){

        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Book bookArr = list.get(i);
            if(bookArr != null){
                if(bookArr.getBookId().equals(book.getBookId())){
                    index = i;
                    break;
                }
            }
        }

        if(index != -1){
            list.set(index, book);
        }

    }

}
